package JZOffer;

import java.util.HashMap;
import java.util.Map;

// https://leetcode.cn/problems/zai-pai-xu-shu-zu-zhong-cha-zhao-shu-zi-lcof/submissions/
// 在排序数组中查找数字
public class Day004_2 {
    public int search0(int[] nums, int target) {
        Map<Integer, Integer> map = new HashMap<>();
        for (int num : nums) {
            if (!map.containsKey(num)) {
                map.put(num, 1);
            } else {
                map.put(num, map.get(num) + 1);
            }
        }
        return map.getOrDefault(target, 0);
    }

    public int search(int[] nums, int target) {
        int count = 0;
        for (int i = 0; i < nums.length; i++) {
            if (nums[i] == target) {
                while (i < nums.length && nums[i] == target) {
                    count++;
                    i++;
                }
                return count;
            }
        }
        return 0;
    }
}
